//
//  Demonstrate identities as used by the request-reply pattern.  Run this
//  program by itself.  
//

package main

import (
	"fmt"
	"os"
	"zmq"
)

func zmq_main() os.Error {
	context, err := zmq.Init(1)
	if err != nil {
		return err
	}
	defer context.Close()

	sink, err := context.Socket(zmq.ROUTER)
	if err != nil {
		return err
	}
	defer sink.Close()

	err = sink.Bind("inproc://example")
	if err != nil {
		return err
	}

	anonymous, err := context.Socket(zmq.REQ)
	if err != nil {
		return err
	}
	defer anonymous.Close()

	err = anonymous.Connect("inproc://example")
	if err != nil {
		return err
	}

	err = anonymous.Send([]byte("ROUTER uses a generated UUID"), 0)
	if err != nil {
		return err
	}

	err = sink.Dump()
	if err != nil {
		return err
	}

	identities, err := context.Socket(zmq.REQ)
	if err != nil {
		return err
	}
	defer identities.Close()

	err = identities.SetOptionVal(zmq.IDENTITY, "hello")
	if err != nil {
		return err
	}

	err = identities.Connect("inproc://example")
	if err != nil {
		return err
	}

	err = identities.Send([]byte("ROUTER socket uses REQ's socket identity"), 0)
	if err != nil {
		return err
	}

	err = sink.Dump()
	if err != nil {
		return err
	}

	return nil
}

func main() {
	fmt.Println("Demonstrate identities")
	err := zmq_main()
	if err != nil {
		fmt.Println(err)
	}
}
